package com.example.lx5_22.adapter

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.example.base.entity.ParkEntityItem
import com.example.lx5_22.R
import com.example.lx5_22.databinding.ItemParkBinding

class ParkAdapter : PagingDataAdapter<ParkEntityItem,ParkAdapter.ParkViewHolder>(diff){

    private var mOnItemClickListener: OnItemClickListener? = null
    companion object{
        val diff = object : ItemCallback<ParkEntityItem>(){
            override fun areItemsTheSame(
                oldItem: ParkEntityItem,
                newItem: ParkEntityItem
            ): Boolean {
                return oldItem == newItem
            }

            override fun areContentsTheSame(
                oldItem: ParkEntityItem,
                newItem: ParkEntityItem
            ): Boolean {
                return oldItem.id == newItem.id
            }
        }

    }

    class ParkViewHolder(view:View) : ViewHolder(view){
        val binding = DataBindingUtil.bind<ItemParkBinding>(view)
    }

    override fun onBindViewHolder(holder: ParkViewHolder, position: Int) {
        holder.binding?.park = getItem(position)
        if(getItem(position)?.spaceStatus == "空闲"){
            holder.binding?.tvStatus?.setTextColor(Color.GREEN)
            holder.binding?.tvStatus?.setBackgroundResource(R.drawable.shape_park1)
        }else if(getItem(position)?.spaceStatus == "已占用"){
            holder.binding?.tvStatus?.setTextColor(Color.BLUE)
            holder.binding?.tvStatus?.setBackgroundResource(R.drawable.shape_park2)
        }else if(getItem(position)?.spaceStatus == "维护中"){
            holder.binding?.tvStatus?.setTextColor(Color.YELLOW)
            holder.binding?.tvStatus?.setBackgroundResource(R.drawable.shape_park3)
        }

        holder.itemView.setOnClickListener {
            ARouter.getInstance().build("/app/QueryParkActivity")
                .withInt("parkId",getItem(position)?.id!!)
                .withString("status",getItem(position)?.spaceStatus)
                .navigation()
        }

    }

    fun setOnItemClickListener(listener: OnItemClickListener?) {
        this.mOnItemClickListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ParkViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_park, parent, false)
        return ParkViewHolder(view)
    }

}

interface OnItemClickListener {
    fun onItemClick(position: Int)
}